{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Strobogrammatic Number"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #two-pointers #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #双指针 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: isStrobogrammatic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #中心对称数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>中心对称数是指一个数字在旋转了&nbsp;180 度之后看起来依旧相同的数字（或者上下颠倒地看）。</p>\n",
    "\n",
    "<p>请写一个函数来判断该数字是否是中心对称数，其输入将会以一个字符串的形式来表达数字。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1:</strong></p>\n",
    "\n",
    "<pre><strong>输入:</strong> num = &quot;69&quot;\n",
    "<strong>输出:</strong> true\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2:</strong></p>\n",
    "\n",
    "<pre><strong>输入:</strong> num = &quot;88&quot;\n",
    "<strong>输出:</strong> true</pre>\n",
    "\n",
    "<p><strong>示例 3:</strong></p>\n",
    "\n",
    "<pre><strong>输入:</strong> num = &quot;962&quot;\n",
    "<strong>输出:</strong> false</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>num = &quot;1&quot;\n",
    "<strong>输出：</strong>true\n",
    "</pre>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [strobogrammatic-number](https://leetcode.cn/problems/strobogrammatic-number/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [strobogrammatic-number](https://leetcode.cn/problems/strobogrammatic-number/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"69\"', '\"88\"', '\"962\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        # 双指针解法\n",
    "        h_dict = {'0': '0', '1': '1', '6': '9', '8': '8', '9': '6'}\n",
    "        i, j = 0, len(num) - 1\n",
    "        while i <= j:\n",
    "            if num[i] not in h_dict or num[j] not in h_dict or h_dict[num[i]] != num[j] or h_dict[num[j]] != num[i]:\n",
    "                return False\n",
    "            i += 1\n",
    "            j -= 1\n",
    "        return True\n",
    "\n",
    "    def findStrobogrammatic(self, n: int) -> List[str]:\n",
    "        # # 超出内存限制\n",
    "        # res = []\n",
    "        # nums = [str(i) for i in range(10 ** (n - 1), 10 ** n)]\n",
    "        # if n == 1: nums.insert(0, '0')\n",
    "        # for num in nums:\n",
    "        #     if self.isStrobogrammatic(num):\n",
    "        #         res.append(num)\n",
    "        # return res\n",
    "\n",
    "        # 回溯法\n",
    "        def helper(m, n):\n",
    "            if m == 0: return ['']\n",
    "            if m == 1: return ['0', '1', '8']\n",
    "            # 递归步骤，生成较小长度的中心对称数\n",
    "            middles = helper(m - 2, n)\n",
    "            result = []\n",
    "            for middle in middles:\n",
    "                # 对于不是最外层的中心对称数，可以在外侧加'0'\n",
    "                if m != n:\n",
    "                    result.append('0' + middle + '0')\n",
    "                # 添加其他中心对称数的组合\n",
    "                result.append('1' + middle + '1')\n",
    "                result.append('6' + middle + '9')\n",
    "                result.append('8' + middle + '8')\n",
    "                result.append('9' + middle + '6')\n",
    "            return result\n",
    "        # m：当前要生成的中心对称数的长度\n",
    "        # n：原始输入的长度，即最初的n。\n",
    "        return helper(n, n)\n",
    "\n",
    "        # # 回溯法\n",
    "        # def helper(left, right, tmp = [None] * n, res = tuple()):\n",
    "        #     if left > right: return (''.join(tmp), )\n",
    "        #     for k, v in (('0', '0'), ('1', '1'), ('6', '9'), ('8', '8'), ('9', '6')):\n",
    "        #         if not left and right and k == '0' or left == right and k != v: continue\n",
    "        #         tmp[left], tmp[right] = k, v\n",
    "        #         res += helper(left + 1, right - 1)\n",
    "        #     print(res)\n",
    "        #     return res\n",
    "        # return helper(0, n - 1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        sto_num ={'0':0,'1':1,'6':9,'8':8,'9':6}\n",
    "        rotate = 0\n",
    "        for _ in num[::-1]:\n",
    "            if sto_num.get(_) is None:\n",
    "                return False\n",
    "            rotate = (rotate*10)+sto_num[_]\n",
    "        return str(rotate) == num        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "\n",
    "        pairs = defaultdict(str)\n",
    "        pairs.update({\n",
    "            '0': '0',\n",
    "            '1': '1',\n",
    "            '6': '9',\n",
    "            '8': '8',\n",
    "            '9': '6',\n",
    "        })\n",
    "\n",
    "        n = len(num)\n",
    "        if n % 2 == 1 and num[n // 2] not in ['0', '1', '8']:\n",
    "            return False\n",
    "\n",
    "        l, r = 0, n - 1\n",
    "\n",
    "        while l < r:\n",
    "            if pairs[num[l]] != num[r]:\n",
    "                return False\n",
    "            l += 1\n",
    "            r -= 1\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        from collections import deque\n",
    "        rotation_dict = {\n",
    "            '9': '6',\n",
    "            '8': '8',\n",
    "            '6': '9',\n",
    "            '1': '1',\n",
    "            '0': '0'\n",
    "        }\n",
    "\n",
    "        invert_num_list = deque()\n",
    "        for item in num:\n",
    "            if item not in rotation_dict:\n",
    "                return False\n",
    "            invert_num_list.appendleft(rotation_dict[item])\n",
    "        \n",
    "        if ''.join(invert_num_list) == num:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        # 满足旋转后还是数字的数字\n",
    "        reverseDict = {'6': '9', '9': '6', '8': '8', '0': '0', '1': '1'}\n",
    "        l, r = 0, len(num) - 1\n",
    "        while l <= r:\n",
    "            if num[l] not in reverseDict or num[r] not in reverseDict or reverseDict[num[l]] != num[r]:\n",
    "                return False\n",
    "            \n",
    "            l += 1\n",
    "            r -= 1\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        n=len(num)-1\n",
    "        for i in range(n//2+1):\n",
    "            if num[i]=='6' and num[n-i]=='9':\n",
    "                continue\n",
    "            elif num[i]=='9'and num[n-i]=='6':\n",
    "                continue\n",
    "            elif num[i]=='8' and num[n-i]=='8':\n",
    "                continue\n",
    "            elif num[i]=='1' and num[n-i]=='1':\n",
    "                continue\n",
    "            elif num[i]=='0' and num[n-i]=='0':\n",
    "                continue\n",
    "            else:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def isStrobogrammatic(self, num):\n",
    "        \"\"\"\n",
    "        :type num: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    " \n",
    "        #如 0, 1, 6, 8, 9 旋转 180° 以后，得到了新的数字 0, 1, 9, 8, 6 。\n",
    "        #2, 3, 4, 5, 7 旋转 180° 后,得到的不是数字。\n",
    "        if not num:\n",
    "            return True\n",
    "        mapping = {0:0, 1:1, 6:9, 8:8, 9:6}\n",
    "        invalid = [2,3,4,5,7]\n",
    "        \n",
    "        N = int(num)\n",
    "        n = N\n",
    "        tmp = 0\n",
    "        res = list()\n",
    "        while(n):\n",
    "            n, tmp = divmod(n, 10)\n",
    "            if tmp in invalid:\n",
    "                return False\n",
    "            res.append(mapping[tmp])\n",
    "        \n",
    "        res = res[::-1]\n",
    "        r = 0\n",
    "        for i, x in enumerate(res):\n",
    "            r += 10 ** i * x\n",
    "        \n",
    "        return r == N"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        \n",
    "        n = len(num)\n",
    "        valid_pairs = {(\"0\",\"0\"), (\"1\",\"1\"), (\"8\",\"8\"), (\"6\",\"9\"), (\"9\",\"6\")}\n",
    "\n",
    "        for i in range(n // 2 + 1):\n",
    "            if (num[i], num[n - 1 - i]) not in valid_pairs:\n",
    "                return False\n",
    "        \n",
    "        return True\n",
    "\n",
    "        \"\"\"\n",
    "        map = {\"0\":\"0\", \"1\":\"1\", \"8\":\"8\", \"6\":\"9\", \"9\":\"6\"}\n",
    "        for i in range(n//2 + 1):  \n",
    "                  \n",
    "            left = num[i]\n",
    "            right = num[n - 1- i]\n",
    "\n",
    "            if i == n - 1 - i:\n",
    "                if left not in map or left == \"6\" or left == \"9\":\n",
    "                    return False\n",
    "            elif left not in map:\n",
    "                return False\n",
    "            elif right != map[left]:\n",
    "                return False\n",
    "\n",
    "        return True    \n",
    "        \"\"\" "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        def good(a, b):\n",
    "            a, b = int(a), int(b)\n",
    "            p1 = a == b and a in (8, 1, 0)\n",
    "            p2 = a == 9 and b == 6\n",
    "            p3 = a == 6 and b == 9\n",
    "            re = [p1, p2, p3]\n",
    "            # print(a, b, re)\n",
    "            return any(re)\n",
    "\n",
    "        if not num:\n",
    "            return True\n",
    "\n",
    "        i, j = 0, len(num) - 1\n",
    "        while i <= j:\n",
    "            if not good(num[i], num[j]):\n",
    "                return False\n",
    "            i += 1\n",
    "            j -= 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "69 - 96 True\n",
    "6 - 9 False\n",
    "反转后不对应，就是false\n",
    "\"\"\"\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "\n",
    "        stro_map = {\n",
    "            \"0\": \"0\",\n",
    "            \"1\": \"1\",\n",
    "            \"6\": \"9\",\n",
    "            \"8\": \"8\",\n",
    "            \"9\": \"6\"\n",
    "        }\n",
    "\n",
    "        left, right = 0, len(num) - 1\n",
    "\n",
    "        while left <= right:\n",
    "            # 如果一个不再存在stro_map中\n",
    "            # 或者他翻转后不是对应的6-9, 说明一整个num旋转后也不符合条件\n",
    "            # 隐式条件: num[right] 和num[left]对应，就都应该在stro_map\n",
    "            if num[left] not in stro_map or stro_map[num[left]] != num[right]:\n",
    "                return False\n",
    "            left += 1\n",
    "            right -= 1\n",
    "\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "\n",
    "        stro_map = {\n",
    "            \"0\": \"0\",\n",
    "            \"1\": \"1\",\n",
    "            \"6\": \"9\",\n",
    "            \"8\": \"8\",\n",
    "            \"9\": \"6\"\n",
    "        }\n",
    "\n",
    "        left, right = 0, len(num) - 1\n",
    "\n",
    "        while left <= right:\n",
    "            # 如果一个不再存在stro_map中\n",
    "            # 或者他翻转后不是对应的6-9, 说明一整个num旋转后也不符合条件\n",
    "            # 隐式条件: num[right] 和num[left]对应，就都应该在stro_map\n",
    "            if num[left] not in stro_map or stro_map[num[left]] != num[right]:\n",
    "                return False\n",
    "            left += 1\n",
    "            right -= 1\n",
    "\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        for index,value in enumerate(num):\n",
    "            if value !=\"8\" and value !=\"1\" and value !=\"6\" and value !=\"9\" and value !=\"0\":\n",
    "                return False\n",
    "            elif value == \"6\" and index <= int(len(num)/2 - 1):\n",
    "                if num[(index+1)*-1] != \"9\":\n",
    "                    return False\n",
    "            elif value == \"9\" and index <= int(len(num)/2 - 1):\n",
    "                if num[(index+1)*-1] != \"6\":\n",
    "                    return False\n",
    "            elif value == \"1\" and index <= int(len(num)/2 - 1):\n",
    "                if num[(index+1)*-1] != \"1\":\n",
    "                    return False\n",
    "            elif value == \"8\" and index <= int(len(num)/2 - 1):\n",
    "                if num[(index+1)*-1] != \"8\":\n",
    "                    return False\n",
    "            elif value == \"0\" and index <= int(len(num)/2 - 1):\n",
    "                if num[(index+1)*-1] != \"0\":\n",
    "                    return False\n",
    "            elif len(num)%2 ==1 and num[int((len(num)-1)/2)] == \"6\":\n",
    "                return False\n",
    "            elif len(num)%2 ==1 and num[int((len(num)-1)/2)] == \"9\":\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        h = {'1':'1', '6':'9', '8':'8', '9':'6', '0':'0'}\n",
    "        n = len(num)\n",
    "        res = all((num[~i] in h and num[i]==h[num[~i]]) and (num[i] in h and num[~i]==h[num[i]]) for i in range(n//2+1))\n",
    "        return res\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        dic = {'0':'0', '1':'1', '6':'9', '8': '8', '9':'6'}\n",
    "        res = ''\n",
    "        for x in num:\n",
    "            if x not in dic:\n",
    "                return False\n",
    "            res += dic[x]\n",
    "        return num == res[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        h = {'1':'1', '6':'9', '8':'8', '9':'6', '0':'0'}\n",
    "        l = ['1', '0', '8']\n",
    "        n = len(num)\n",
    "\n",
    "        if n%2==1:\n",
    "            if num[n//2] not in l:\n",
    "                print('000')\n",
    "                return False\n",
    "        \n",
    "        for i in range(n//2):\n",
    "            if num[i] not in h:\n",
    "                print('111')\n",
    "                return False\n",
    "            else:\n",
    "                if num[n-i-1]!=h[num[i]]:\n",
    "                    print('222', num[i], num[n-1-i])\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        left=0\n",
    "        if len(num)%2!=0:\n",
    "            right=len(num)//2+1\n",
    "            if int(num[right-1]) not in [1,0,8]:\n",
    "                return False\n",
    "        right=len(num)-1\n",
    "        if len(num)==1:\n",
    "            return True\n",
    "        while left<right:\n",
    "            if num[right]!=num[left]:\n",
    "                if (num[right]=='6' and num[left]=='9')or (num[left]=='6'and num[right]=='9'):\n",
    "                    pass\n",
    "                else:\n",
    "                    return False\n",
    "            else:\n",
    "                if int(num[right]) not in [0,8,1]:\n",
    "                    return False\n",
    "            left+=1\n",
    "            right-=1\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        #输出的是一个字符串,字典指定所需反转的数字\n",
    "        dict={'1':'1',\n",
    "                '9':'6',\n",
    "                '6':'9',\n",
    "                '8':'8' ,\n",
    "                '0':'0'}\n",
    "        ls=''\n",
    "        for i in num:\n",
    "            if i in dict:\n",
    "                ls+=dict[i]\n",
    "            else:\n",
    "                return False\n",
    "        if ls[::-1]==num:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        hashmap = {'0': '0', '1': '1', '6': '9', '8': '8', '9': '6'}\n",
    "        n = len(num)\n",
    "        t = n // 2\n",
    "        for i in range(t):\n",
    "            if hashmap.get(num[i], None) != num[n - i - 1]:\n",
    "                return False\n",
    "        if n % 2:\n",
    "            if hashmap.get(num[t], None) != num[t]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        num_r = num[::-1]\n",
    "        mapping = {1:1,8:8,6:9,9:6,0:0}\n",
    "        stro_num = list(mapping.keys())\n",
    "        for x,y in zip(num,num_r):\n",
    "            if int(x) not in stro_num:\n",
    "                return False\n",
    "            else:\n",
    "                if mapping[int(x)]!=int(y):\n",
    "                    return False\n",
    "        return True  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        d = {'6':'9','9':'6','8':'8','1':'1','0':'0'}\n",
    "        if any(i not in d for i in num):\n",
    "            return False\n",
    "        for i in range(len(num)//2):\n",
    "            if d[num[i]]!=num[-i-1]:\n",
    "                return False\n",
    "        if len(num)%2==1:\n",
    "            return num[len(num)//2] in '018'\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        num_map = {'0':'0','1':'1','6':'9','9':'6','8':'8'}\n",
    "        n = len(num)\n",
    "        for i in range((n+1)//2):\n",
    "            left,right = num[i],num[n-1-i]\n",
    "            if left not in num_map or num_map[left] != right:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        \n",
    "        # 在 Python 中，我们使用一个列表作为一个 string builder。\n",
    "        rotated_string_builder = []\n",
    "        \n",
    "        # 请记住，我们希望在字符串中向后循环。\n",
    "        ### reversed(num) 意思是前后逆转，下面进行的是上下逆转\n",
    "        for c in reversed(num):\n",
    "            if c in {'0', '1', '8'}:\n",
    "                rotated_string_builder.append(c)\n",
    "            elif c == '6':\n",
    "                rotated_string_builder.append('9')\n",
    "            elif c == '9':\n",
    "                rotated_string_builder.append('6')\n",
    "            else: # 这一定是无效的数字。\n",
    "                return False\n",
    "        \n",
    "        # 在 Python 中，我们使用 join 来转变一个字符列表为字符串\n",
    "        rotated_string = \"\".join(rotated_string_builder)\n",
    "        return rotated_string == num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        hashtable = {'0':'0', '1':'1', '6':'9', '8':'8', '9':'6'}\n",
    "        left, right = 0, len(num) - 1\n",
    "        while left <= right:\n",
    "            if hashtable.get(num[left], '-') != num[right]:\n",
    "                return False\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        map_dict = {'1':'1', '6': '9', '8': '8', '9': '6', '0': '0'}\n",
    "        reverse_list = []\n",
    "        for item in num:\n",
    "            if item not in map_dict:\n",
    "                return False\n",
    "            reverse_list.append(map_dict[item])\n",
    "        reverse_str = ''.join(reverse_list[:: -1])\n",
    "        return num == reverse_str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        hashtable = {'0':'0', '1':'1', '6':'9', '8':'8', '9':'6'}\n",
    "        left, right = 0, len(num) - 1\n",
    "        while left <= right:\n",
    "            if hashtable.get(num[left], '-') != num[right]:\n",
    "                return False\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        rotated_digits = {'0': '0', '1': '1', '8': '8', '6': '9', '9': '6'}\n",
    "        \n",
    "        rotated_string_builder = []\n",
    "        \n",
    "        for c in reversed(num):\n",
    "            if c not in rotated_digits:\n",
    "                return False\n",
    "            rotated_string_builder.append(rotated_digits[c])\n",
    "        \n",
    "        rotated_string = \"\".join(rotated_string_builder)\n",
    "        return rotated_string == num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        mapping: Dict = {\n",
    "            '0': '0',\n",
    "            '1': '1',\n",
    "            '6': '9',\n",
    "            '8': '8',\n",
    "            '9': '6',\n",
    "        }\n",
    "\n",
    "        l:int = 0\n",
    "        \n",
    "        while l <= len(num) // 2:\n",
    "            if num[l] not in mapping:\n",
    "                return False \n",
    "            if mapping[num[l]] != num[len(num) - 1 -l]:\n",
    "                return False \n",
    "            l += 1\n",
    "        else:\n",
    "            return True \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        dict_rotate = {'1':'1','0':'0','6':'9','9':'6','8':'8'}\n",
    "        mid = len(num)//2 + 1\n",
    "        for i in range(mid):\n",
    "            if num[i] not in dict_rotate.keys():\n",
    "                return False\n",
    "            if num[len(num)-1-i] == dict_rotate[num[i]]:\n",
    "                continue\n",
    "            else:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    sym = {\"0\": \"0\", \"1\": \"1\", \"8\": \"8\", \"6\": \"9\", \"9\": \"6\"}\n",
    "    \n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        sym = self.sym\n",
    "\n",
    "        l = 0\n",
    "        r = len(num) - 1\n",
    "\n",
    "        while l <= r:\n",
    "            x, y = num[l], num[r]\n",
    "            if x not in sym or y not in sym or sym[x] != y:\n",
    "                return False\n",
    "            l += 1\n",
    "            r -= 1\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        left = 0\n",
    "        right = len(num) - 1\n",
    "        while left <= right:\n",
    "            if num[left] == \"6\" and num[right] == \"9\":\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            elif num[left] == \"9\" and num[right] == \"6\":\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            elif num[left] == num[right] and num[left] == \"8\":\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            elif num[left] == num[right] and num[left] == \"1\":\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            elif num[left] == num[right] and num[left] == \"0\":\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            else:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "      dict={'1':'1','9':'6','6':'9','8':'8','0':'0'}\n",
    "      ls=''\n",
    "      for i in num:\n",
    "        if i in dict:\n",
    "          ls+=dict[i]\n",
    "        else:\n",
    "          return False\n",
    "      if ls[::-1] == num:\n",
    "        return True\n",
    "      else:\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "\n",
    "        pairs = defaultdict(str)\n",
    "        pairs.update({\n",
    "            '0': '0',\n",
    "            '1': '1',\n",
    "            '6': '9',\n",
    "            '8': '8',\n",
    "            '9': '6',\n",
    "        })\n",
    "\n",
    "        n = len(num)\n",
    "        if n % 2 == 1 and num[n // 2] not in ['0', '1', '8']:\n",
    "            return False\n",
    "\n",
    "        l, r = 0, n - 1\n",
    "\n",
    "        while l < r:\n",
    "            if pairs[num[l]] != num[r]:\n",
    "                return False\n",
    "            l += 1\n",
    "            r -= 1\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        \n",
    "        dic={\"0\":\"0\",\"1\":\"1\",\"6\":\"9\",\"8\":\"8\",\"9\":\"6\"}\n",
    "        l=list(num)\n",
    "        for i in range(len(l)):\n",
    "            if l[i] in dic:\n",
    "                l[i]=dic[l[i]]\n",
    "            else:\n",
    "                return False\n",
    "        l=l[::-1]\n",
    "        if \"\".join(l)==num:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        n=len(num)\n",
    "        al=['0','1','6','8','9']\n",
    "        for i in num:\n",
    "            if i not in al:\n",
    "                return False\n",
    "        dt={}\n",
    "        dt['0']='0'\n",
    "        dt['1']='1'\n",
    "        dt['6']='9'\n",
    "        dt['9']='6'\n",
    "        dt['8']='8'\n",
    "\n",
    "        mid,left,right='','',''\n",
    "        if n%2==1:\n",
    "            mid=num[n//2]\n",
    "            left=num[0:n//2]\n",
    "            right=num[n//2+1:]\n",
    "            if mid not in ['0','1','8']: return False\n",
    "        else:\n",
    "            left=num[0:n//2]\n",
    "            right=num[n//2:]\n",
    "        print(left,mid,right)\n",
    "        #旋转\n",
    "        left=left[::-1]\n",
    "        nl=''\n",
    "        for i in range(len(left)):\n",
    "            nl=nl+dt[left[i]]\n",
    "        \n",
    "        if nl==right:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        \n",
    "        # 在 Python 中，我们使用一个列表作为一个 string builder。\n",
    "        rotated_string_builder = []\n",
    "        \n",
    "        # 请记住，我们希望在字符串中向后循环。\n",
    "        for c in reversed(num):\n",
    "            if c in {'0', '1', '8'}:\n",
    "                rotated_string_builder.append(c)\n",
    "            elif c == '6':\n",
    "                rotated_string_builder.append('9')\n",
    "            elif c == '9':\n",
    "                rotated_string_builder.append('6')\n",
    "            else: # 这一定是无效的数字。\n",
    "                return False\n",
    "        \n",
    "        # 在 Python 中，我们使用 join 来转变一个字符列表为字符串\n",
    "        rotated_string = \"\".join(rotated_string_builder)\n",
    "        return rotated_string == num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        flag = False\n",
    "        temp_list = []\n",
    "        for obj in num:\n",
    "            # print(obj)\n",
    "            if obj in [\"2\", \"3\", \"4\", \"5\", \"7\"]:\n",
    "                temp_list.append(\"None\")\n",
    "            elif obj == \"0\":\n",
    "                temp_list.append(\"0\")\n",
    "            elif obj == \"1\":\n",
    "                temp_list.append(\"1\")\n",
    "            elif obj == \"6\":\n",
    "                temp_list.append(\"9\")\n",
    "            elif obj == \"8\":\n",
    "                temp_list.append(\"8\")\n",
    "            elif obj == \"9\":\n",
    "                temp_list.append(\"6\")\n",
    "        reverse_str = \"\".join(temp_list[::-1])\n",
    "        print(\"reverse_str\", reverse_str)\n",
    "\n",
    "        if reverse_str == num:\n",
    "            flag = True\n",
    "        return flag\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        \"\"\"\n",
    "        6--9; 8--8; 1--1;0--0\n",
    "        \"\"\"\n",
    "        # 只有上面几个旋转180度后跟原数一样的。\n",
    "        # 双指针\n",
    "        # t = {\"6\":\"9\", \"9\":\"6\", \"8\":\"8\", \"1\":\"1\", \"0\":\"0\"}\n",
    "        # left = 0\n",
    "        # right = len(num) - 1\n",
    "        # while left < right:\n",
    "        #     if num[left] not in t or num[right] not in t:\n",
    "        #         return False\n",
    "        #     if t[num[left]] != num[right]:\n",
    "        #         return False\n",
    "        #     left += 1\n",
    "        #     right -= 1\n",
    "        # return True\n",
    "\n",
    "        # 构造一个反转数\n",
    "        rotate = \"\"\n",
    "        for x in num:\n",
    "            if x==\"0\" or x==\"1\" or x==\"8\":\n",
    "                rotate = x + rotate\n",
    "            elif x==\"6\":\n",
    "                rotate = \"9\" + rotate\n",
    "            elif x==\"9\":\n",
    "                rotate = \"6\" + rotate\n",
    "            else:\n",
    "                return False\n",
    "        return num == rotate\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        dic = {\"6\" : \"9\", \"1\" : \"1\", \"8\" : \"8\", \"0\" : \"0\", \"9\" : \"6\"}\n",
    "        right = len(num) - 1\n",
    "        left = 0\n",
    "        while left <= right:\n",
    "            if num[left] not in dic or num[right] not in dic:\n",
    "                return False\n",
    "            if dic[num[left]] == num[right]:\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            else:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        m = {'0':'0', '1':'1', '6':'9', '8':'8', '9':'6'}\n",
    "        for i in range(len(num) // 2 + 1):\n",
    "            if num[i] not in m or m[num[i]] != num[len(num) - i - 1]:\n",
    "                return False\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        \"\"\"\n",
    "        6--9; 8--8; 1--1;0--0\n",
    "        \"\"\"\n",
    "        # 只有上面几个旋转180度后跟原数一样的。\n",
    "        # 双指针\n",
    "        # t = {\"6\":\"9\", \"9\":\"6\", \"8\":\"8\", \"1\":\"1\", \"0\":\"0\"}\n",
    "        # left = 0\n",
    "        # right = len(num) - 1\n",
    "        # while left < right:\n",
    "        #     if num[left] not in t or num[right] not in t:\n",
    "        #         return False\n",
    "        #     if t[num[left]] != num[right]:\n",
    "        #         return False\n",
    "        #     left += 1\n",
    "        #     right -= 1\n",
    "        # return True\n",
    "\n",
    "        # 构造一个反转数\n",
    "        # rotate = \"\"\n",
    "        # for x in num:\n",
    "        #     if x==\"0\" or x==\"1\" or x==\"8\":\n",
    "        #         rotate = x + rotate\n",
    "        #     elif x==\"6\":\n",
    "        #         rotate = \"9\" + rotate\n",
    "        #     elif x==\"9\":\n",
    "        #         rotate = \"6\" + rotate\n",
    "        #     else:\n",
    "        #         return False\n",
    "        # return num == rotate\n",
    "\n",
    "\n",
    "        # 国外\n",
    "        rotated = { n : r for r,n in zip( '01689', '01986' ) }\n",
    "        print(rotated)\n",
    "\n",
    "        if any( digit not in rotated for digit in num ):\n",
    "            return False\n",
    "\n",
    "        rNum = ''.join( rotated[ digit ] for digit in reversed( num ) )\n",
    "    \n",
    "        return num == rNum\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        stro_pair = [[\"0\", \"0\"], [\"1\", \"1\"], [\"8\", \"8\"], [\"6\", \"9\"], [\"9\", \"6\"]]\n",
    "        start, end = 0, len(num) - 1\n",
    "        while start <= end:\n",
    "            pair = [num[start], num[end]]\n",
    "            if pair not in stro_pair:\n",
    "                return False\n",
    "            start += 1\n",
    "            end -= 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        tar=[0,1,2,3,4,5,9,7,8,6]\n",
    "        valid=[2,3,4,5,7]\n",
    "        n=int(num)\n",
    "        ns=0\n",
    "        while n>0:\n",
    "            last=n%10\n",
    "            if last in valid:\n",
    "                return False\n",
    "            else:\n",
    "                ns=ns*10+tar[last]\n",
    "                n=n//10\n",
    "        return ns==int(num)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        dic = {\"0\": 0, \"1\": 1, \"6\": 9, \"8\": 8, \"9\": 6}\n",
    "        tar = [0, 1, 2, 3, 4, 5, 9, 7, 8, 6]\n",
    "        valid = [2, 3, 4, 5, 7]\n",
    "        n=int(num)\n",
    "        ns=0\n",
    "        while n:\n",
    "            t=n%10\n",
    "            if t in valid:\n",
    "                return False\n",
    "            else:\n",
    "                ns=ns*10+tar[t]\n",
    "                n//=10\n",
    "        print(ns)\n",
    "        return ns==int(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        hashtable={'0':'0','1':'1','6':'9','8':'8','9':'6'}\n",
    "        for i in range(0,(len(num)+1)//2,1):\n",
    "            j = len(num) -i-1\n",
    "            if(num[j] != hashtable.get(num[i],'-')):\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isStrobogrammatic(self, num: str) -> bool:\n",
    "        dict1={\"1\":\"1\",\"6\":\"9\",\"8\":\"8\",\"9\":\"6\",\"0\":\"0\"}\n",
    "        if len(num)==1:\n",
    "            if num[0]==\"8\" or num[0]==\"1\" or num[0]==\"0\":\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "\n",
    "        for i in range(round(len(num)/2)+1):\n",
    "            if (num[i] not in dict1) or (num[len(num)-i-1] not in dict1):\n",
    "                return False\n",
    "            if num[i]!=dict1[num[len(num)-i-1]]:\n",
    "                \n",
    "            \n",
    "                return False\n",
    "         \n",
    "        return True\n",
    "        \n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
